home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / os2tools / aping / apingd.c < prev    next >
Text File  |  1992-06-14  |  13KB  |  371 lines

  1. /*****************************************************************************
  2.  *
  3.  *  MODULE NAME : APINGD.C
  4.  *
  5.  *  COPYRIGHTS:
  6.  *             This module contains code made available by IBM
  7.  *             Corporation on an AS IS basis.  Any one receiving the
  8.  *             module is considered to be licensed under IBM copyrights
  9.  *             to use the IBM-provided source code in any way he or she
  10.  *             deems fit, including copying it, compiling it, modifying
  11.  *             it, and redistributing it, with or without
  12.  *             modifications.  No license under any IBM patents or
  13.  *             patent applications is to be implied from this copyright
  14.  *             license.
  15.  *
  16.  *             A user of the module should understand that IBM cannot
  17.  *             provide technical support for the module and will not be
  18.  *             responsible for any consequences of use of the program.
  19.  *
  20.  *             Any notices, including this one, are not to be removed
  21.  *             from the module without the prior written consent of
  22.  *             IBM.
  23.  *
  24.  *  AUTHOR:    Peter J. Schwaller
  25.  *             VNET:     PJS at RALVM6           Tie Line: 444-4376
  26.  *             Internet: pjs@ralvm6.vnet.ibm.com     (919) 254-4376
  27.  *
  28.  *  FUNCTION:  Perform an echo test to a specified LU.
  29.  *             APING can be used when you are first installing APPC on
  30.  *             your computer to make sure you can connect to another
  31.  *             computer in the network.  APING can also be used to
  32.  *             get an estimate of the delay time or throughput to another
  33.  *             computer in the network.
  34.  *
  35.  *             APINGD echos whatever is sent by APING.
  36.  *                Keep receiving until you get permission to send
  37.  *                Send the same number of same size records
  38.  *
  39.  *  AVAILABILITY:
  40.  *             These sample programs and source are also available on
  41.  *             CompuServe through the APPC Information Exchange.  To get
  42.  *             to the APPC forum just type 'GO APPC' from any CompuServe
  43.  *             prompt.  The samples are available in the Sample Programs
  44.  *             library section.  Just search on the keyword CPICPGMS to
  45.  *             find all the samples in this series.
  46.  *
  47.  *             Updates for the sample programs and support for many more
  48.  *             CPI-C platforms will also be made available on CompuServe.
  49.  *
  50.  *  RELATED FILES:
  51.  *             See APING.DOC for usage instructions.
  52.  *
  53.  *****************************************************************************/
  54.  
  55.  
  56. /* My CPI-C include file */
  57. /* Hides CMC.H differences among platforms */
  58. #include "cpiccmc.h"
  59.  
  60. /* standard C include files */
  61. #include <stdio.h>
  62. #include <stdlib.h>
  63. #include <string.h>
  64.  
  65. /* Collection of routines with special ported version for each platform */
  66. #include "cpicport.h"
  67.  
  68. /* CPI-C error handling routines */
  69. /* This file is supplied with APINGD */
  70. #include "cpicerr.h"
  71.  
  72. /* CPI-C initialization routines */
  73. /* This file is supplied with APINGD */
  74. #include "cpicinit.h"
  75.  
  76. /* Argument processing procedure */
  77. /* This file is supplied with APINGD */
  78. #include "getopt.h"
  79.  
  80. /* CPI-C error handling info */
  81. CPICERR * cpicerr;
  82.  
  83. /*
  84.  * Max size of a data buffer.  This is the largest size buffer that can
  85.  * be specified on a call to CPI-C.
  86.  */
  87. #define  MAX_SIZE (0x7FFF)
  88.  
  89. /* Define these here so we can make changes throughout the code. */
  90. #define  PROGRAM_NAME      "APINGD"
  91. #define  PROGRAM_INFO      "version 2.2"
  92. #define  MAJOR_VERSION     (2)
  93. #define  MINOR_VERSION     (2)
  94. #define  LOG_FILE_NAME     "apingd.err"
  95. #define  LOG_FILE_PATH     "$LOGPATH"
  96.  
  97. /*
  98.  * Message displayed with show_info() when APINGD is started.
  99.  */
  100. char * intro[] = {
  101.     PROGRAM_NAME " " PROGRAM_INFO " - APPC loopback server",
  102.     NULL
  103.     };
  104.  
  105. char * log_file_name = NULL;
  106.  
  107.  
  108.  
  109. CM_ENTRY cmsltp(
  110.                 unsigned char CM_PTR,   /* TP_name                  */
  111.                 CM_INT32 CM_PTR,        /* TP_name_length           */
  112.                 CM_INT32 CM_PTR);       /* return_code              */
  113.  
  114. CM_ENTRY cmetpn(unsigned char CM_PTR,   /* conversation_ID          */
  115.                 unsigned char CM_PTR,   /* tp_name                  */
  116.                 CM_INT32 CM_PTR,        /* tp_name_length           */
  117.                 CM_INT32 CM_PTR);       /* return_code              */
  118.  
  119.  
  120.  
  121. void cdecl
  122. main( int argc, char *argv[])
  123. {
  124.     /* Variables used for CPI-C calls */
  125.     unsigned char cm_conv_id[8];            /* CPI-C conversation ID         */
  126.     CM_INT32    cm_rc;                      /* CPI-C return code             */
  127.     CM_INT32    length;                     /* generic length variable       */
  128.     CM_INT32    rts_received;               /* request to send received      */
  129.     CM_INT32    max_receive_len;            /* Max receive length on CMRCV   */
  130.     CM_INT32    what_received;              /* What received parm from CMRCV */
  131.     CM_INT32    received_len;               /* Amount of data rcvd on CMRCV  */
  132.     CM_INT32    status_received;            /* Status from CMRCV             */
  133.  
  134.     /* Data buffer for send and receive */
  135.     unsigned char CM_PTR buffer;            /* CPIC data buffer              */
  136.  
  137.     char        destination[MAX_DESTINATION];/* Partner destination          */
  138.     USHORT      max_size;                   /* size to receive               */
  139.     int         c;                          /* flag specifed, used w/getopt  */
  140.  
  141.     char partner_major_version;
  142.     char partner_minor_version;
  143.  
  144.  
  145.     show_info(intro);                       /* display intro information     */
  146.  
  147.     while (optind != argc) {
  148.         c = getopt(argc, argv, "?l:t:");
  149.         switch (c) {
  150.         case 't':
  151.         case 'T':
  152.  
  153. {
  154. #if 0
  155. unsigned char tp_name[64+1];
  156. CM_INT32      tp_name_length;
  157.         strcpy(tp_name, optarg);
  158.         tp_name_length = strlen(optarg);
  159.  
  160.         cmsltp(
  161.                (unsigned char *)tp_name,
  162.                &tp_name_length,
  163.                &cm_rc );
  164.  
  165.         tp_name[(int)tp_name_length] = '\0';
  166.         printf("\nMy tp name will be: %s\n", tp_name);
  167.  
  168.  
  169.  
  170. #endif
  171. }
  172.             break;
  173.         case EOF:
  174.             optind++;
  175.             break;
  176.         case 'l':
  177.         case 'L':
  178.             printf("\nIncoming partner LU names will be logged to: %s\n",
  179.                                                                 optarg);
  180.             log_file_name = optarg;
  181.             break;
  182.         } /* endswitch */
  183.     } /* endwhile */
  184.  
  185.     /*
  186.      * Initialize the CPICERR structure.  This is done before the CMACCP
  187.      * call so that we can use CPICERR for help with errors on CMACCP.
  188.      * The procedure is in CPICERR.C
  189.      */
  190.     cpicerr = cpicerr_new();
  191.     cpicerr_set_program_name(cpicerr, PROGRAM_NAME);
  192.     cpicerr_set_program_info(cpicerr, PROGRAM_INFO);
  193.     cpicerr_set_major_version(cpicerr, MAJOR_VERSION);
  194.     cpicerr_set_minor_version(cpicerr, MINOR_VERSION);
  195.     cpicerr_set_log_file_name(cpicerr, LOG_FILE_NAME);
  196.     cpicerr_set_log_file_path(cpicerr, LOG_FILE_PATH);
  197.  
  198.     cmaccp(cm_conv_id,                      /* Accept Conversation           */
  199.            &cm_rc);
  200.  
  201.     /*
  202.      * Fill in conversation information for CPI-C error reporting.
  203.      */
  204.     cpicerr_set_conv_id(cpicerr, cm_conv_id);
  205.  
  206.  
  207.     if (cm_rc != CM_OK) {
  208.         cpicerr_handle_rc(cpicerr, MSG_CMACCP, cm_rc);
  209.     } else {
  210.         CM_INT32 pln_length;
  211.         /*
  212.          * Extract the partner LU name and display it.
  213.          */
  214.         cmepln(cm_conv_id,
  215.                (unsigned char *)destination,
  216.                &pln_length,
  217.                &cm_rc );
  218.  
  219.         destination[(int)pln_length] = '\0';
  220.         printf("\nContacted by partner: %s\n", destination);
  221.  
  222.         if (log_file_name != NULL) {
  223.             FILE * file;
  224.             file = fopen(log_file_name, "a");
  225.             if (file != NULL) {
  226.                 fprintf(file, "\nContacted by partner: %s\n", destination);
  227.                 fclose(file);
  228.             }
  229.         }
  230.  
  231.     }
  232.  
  233. {
  234. #if 0
  235. unsigned char tp_name[64+1];
  236. CM_INT32      tp_name_length;
  237.  
  238.         cmetpn(cm_conv_id,
  239.                (unsigned char *)tp_name,
  240.                &tp_name_length,
  241.                &cm_rc );
  242.  
  243.         tp_name[(int)tp_name_length] = '\0';
  244.         printf("\nMy tp name was: %s\n", tp_name);
  245.  
  246.  
  247. #endif
  248.  
  249. }
  250.  
  251.  
  252.  
  253.     {
  254.     CM_PREPARE_TO_RECEIVE_TYPE prep_to_receive = CM_PREP_TO_RECEIVE_FLUSH;
  255.     cmsptr(cm_conv_id,                       /* Set prepare to receive type  */
  256.            &prep_to_receive,
  257.            &cm_rc);
  258.     if (cm_rc != CM_OK) cpicerr_handle_rc(cpicerr, MSG_CMSPTR, cm_rc);
  259.     }
  260.  
  261.  
  262.     max_receive_len = max_size = MAX_SIZE;
  263.  
  264.     buffer = (unsigned char CM_PTR)alloc_cpic_buffer(max_size);
  265.                                             /* allocate a buffer             */
  266.  
  267.     cpicerr_exchange_version(cpicerr,
  268.                              cm_conv_id,
  269.                              CM_RECEIVE_STATE,
  270.                              &partner_major_version,
  271.                              &partner_minor_version);
  272.  
  273.     do {
  274.         ULONG       count;                  /* number of consecutive         */
  275.                                             /* sends or receives             */
  276.         count = 0;                          /* initialize count of recvs     */
  277.         do {
  278.            cmrcv (cm_conv_id,               /* Receive Data                  */
  279.                   buffer,                   /* Data Pointer                  */
  280.                   &max_receive_len,         /* Size of Data Buffer           */
  281.                   &what_received,           /* returned - what received      */
  282.                   &received_len,            /* returned - length of data     */
  283.                   &status_received,         /* returned - status received    */
  284.                   &rts_received,            /* returned - request to send    */
  285.                   &cm_rc);
  286.  
  287.             if (what_received != CM_NO_DATA_RECEIVED) {
  288.                 count++;                    /* keep track of receives        */
  289.             }
  290.         } while ( (status_received != CM_SEND_RECEIVED) &&
  291.                   (status_received != CM_CONFIRM_RECEIVED) &&
  292.                    !cm_rc);
  293.         /*
  294.          * loop until we get permission to send data or until error
  295.          */
  296.  
  297.         if (cm_rc != CM_OK) {
  298.             if (cm_rc == CM_DEALLOCATED_NORMAL) {
  299.                 exit(EXIT_SUCCESS);
  300.             } else {
  301.                 cpicerr_handle_rc(cpicerr, MSG_CMRCV, cm_rc);
  302.             }
  303.         }
  304.  
  305.         if (status_received != CM_CONFIRM_RECEIVED) {
  306.             /*
  307.              * count is now equal to the number of data blocks we received
  308.              * now we will send back the same number of data blocks of equal
  309.              * size
  310.              */
  311.             {
  312.             CM_SEND_TYPE send_type = CM_BUFFER_DATA;
  313.             cmsst(cm_conv_id,
  314.                   &send_type,
  315.                   &cm_rc);
  316.             if (cm_rc != CM_OK) cpicerr_handle_rc(cpicerr, MSG_CMSST, cm_rc);
  317.             }
  318.  
  319.                                  /* send back the same number except for one */
  320.             for ( count--; count && !cm_rc; count-- ) {
  321.                 length = received_len;
  322.                 cmsend(cm_conv_id,
  323.                        buffer,
  324.                        &length,
  325.                        &rts_received,
  326.                        &cm_rc);
  327.                 if (cm_rc != CM_OK) {
  328.                     cpicerr_handle_rc(cpicerr, MSG_CMSEND, cm_rc);
  329.                 }
  330.             }
  331.  
  332.             /*
  333.              * Set the send type to do a send and a prepare to receive.
  334.              * This will send both the data and the send permission indicator
  335.              * to our partner all at once.
  336.              */
  337.             {
  338.             CM_SEND_TYPE send_type = CM_SEND_AND_PREP_TO_RECEIVE;
  339.             cmsst(cm_conv_id,
  340.                   &send_type,
  341.                   &cm_rc);
  342.             if (cm_rc != CM_OK) cpicerr_handle_rc(cpicerr, MSG_CMSST, cm_rc);
  343.             }
  344.  
  345.             length = received_len;
  346.             cmsend(cm_conv_id,
  347.                    buffer,
  348.                    &length,
  349.                    &rts_received,
  350.                    &cm_rc);
  351.             if (cm_rc != CM_OK) cpicerr_handle_rc(cpicerr, MSG_CMSEND, cm_rc);
  352.         } else {
  353.             /*
  354.              * The partner has requested one way data transfer only.
  355.              * We'll just issue Confirmed, then go back up to receive
  356.              * more data.
  357.              */
  358.             cmcfmd(cm_conv_id,
  359.                    &cm_rc);
  360.             if (cm_rc != CM_OK) cpicerr_handle_rc(cpicerr, MSG_CMCFMD, cm_rc);
  361.         }
  362.     } while (cm_rc == CM_OK);
  363.  
  364.  
  365.     /* destroy the object we created with cpicerr_new() */
  366.     cpicerr_destroy(cpicerr);
  367.  
  368.     exit(EXIT_SUCCESS);
  369. }
  370.  
  371.